const express = require('express');
const router = express.Router();
const db = require('../db'); // 同样引用db.js

// 获取单个动物详情
router.get('/:id', async (req, res) => {
  try {
    const animalId = req.params.id;
    const [rows] = await db.query('SELECT * FROM animals WHERE id = ?', [animalId]);

    if (rows.length === 0) {
      res.status(404).send('Animal not found');
    } else {
      res.status(200).json(rows[0]);
    }
  } catch (error) {
    console.error(error);
    res.status(500).send('Error retrieving data from database.');
  }
});

module.exports = router;